using System;
using System.Collections.Generic;
using System.IO;
using DatabaseUnitTesting.Utilities.Tests;
using NUnit.Framework;

namespace DatabaseUnitTesting.Utilities.DataCompare.Tests
{
    [TestFixture]
    public class XmlDiffReaderTests
    {
        [Test]
        public void FakeFileTest()
        {
            CustomAssert.AssertException(delegate { XmlFileAdapter.Read("fakeFile.xml"); },
                                         typeof(FileNotFoundException));
        }

        [Test]
        public void SimpleReadTest()
        {
            Assert.AreEqual(0, XmlFileAdapter.Read("xmlFiles/noDiff.xml").TableCount);

            Database diffs = XmlFileAdapter.Read("xmlFiles/twoDiff.xml");
            Assert.AreEqual(1, diffs.TableCount);
            IEnumerator<KeyValuePair<Table, int>> tables = diffs.Tables.GetEnumerator();
            tables.MoveNext();
            Assert.AreEqual(2, tables.Current.Key.RowCount);
            tables.MoveNext();
            Assert.IsNull(tables.Current.Key);

            diffs = XmlFileAdapter.Read("xmlFiles/oneDiff.xml");
            Assert.AreEqual(1, diffs.TableCount);
            tables = diffs.Tables.GetEnumerator();
            tables.MoveNext();
            Assert.AreEqual(1, tables.Current.Key.RowCount);
            tables.MoveNext();
            Assert.IsNull(tables.Current.Key);
        }

        [Test]
        public void XmlReaderWriterTest()
        {
            Database diffs = new Database();
            string filename = "C:/Temp/xmlreaderwriter.xml";
            Table td = new Table("foo.bar", "foo.bar");
            Row rd = new Row("ant");
            Column kf = new Column("mon", "key");
            rd.AddColumn(kf);
            td.AddRow(rd);
            diffs.AddTable(td);

            XmlFileAdapter.Write(filename, diffs);
            Assert.IsTrue(diffs.Equals(XmlFileAdapter.Read(filename)));
        }
    }
}